package com.zw.mes.module.shipments.controller;


import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.shipments.*;
import com.zw.mes.entity.technology.DsnCoatSheet;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.shipments.model.DebuggerAll;
import com.zw.mes.module.shipments.repository.InstDebuggingFileRepository;
import com.zw.mes.module.shipments.repository.InstDispatchingRepository;
import com.zw.mes.module.shipments.service.InstDispatchingCreateService;
import com.zw.mes.module.shipments.service.InstShipmentsPlanService;
import com.zw.mes.util.FileUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import java.text.ParseException;
import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 创建安装派工单
 * <p>
 * 权限：
 * @Author: liqingyu
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/create/dispatching")
@SuppressWarnings("all")
public class InstDispatchingCreateController extends BaseController {


    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private InstDispatchingCreateService instDispatchingCreateService;
    @Autowired
    private InstDispatchingRepository instDispatchingRepository;
    @Autowired
    private  InstDebuggingFileRepository instDebuggingFileRepository;


    @GetMapping
//    @RequiresPermissions({"tec:proDetail:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "shipments/InstDispatchingCreate/page";
    }

    /**
     *调试主页
     * @param model
     * @return
     */
    @GetMapping("/debuggingPage")
    public String debuggingPage(Model model) {
        return "shipments/InstDebugging/debuggingPage";
    }
    /**
     *调试主页查询
     * @param model
     * @return
     */
    @GetMapping("/debuggingPageSelect")
    public String debuggingPageSelect(Model model) {
        return "shipments/InstDebuggingSelect/debuggingPageSelect";
    }
    /**
     *提交页面主页
     * @param model
     * @return
     */
    @GetMapping("/submit")
    public String submit(Model model) {
        return "shipments/InstDispatchingSubmit/page";
    }
    /**
     * 提交页面List
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/submitList")
    public JsonResult submitList(InstDispatchingList instDispatchingList) {
        startPage();
        List<InstDispatchingList> list = instDispatchingCreateService.findinstDispatchingQuery(instDispatchingList);
        return jsonResult(list);
    }
    /**
     *查看明细
     * @param model
     * @return
     */
    @GetMapping("/listDetailSelect/{id}")
    public String listDetailSelect(Model model,@PathVariable("id") Integer id) {
        InstDispatchingList instDispatchingList = instDispatchingRepository.getOne(id);
        InstDispatchingDetail instDispatchingDetail = new InstDispatchingDetail();
        instDispatchingDetail.setSheetId(id);
        List<InstDispatchingDetail> instDispatchingDetails = instDispatchingCreateService.findinstDispatchingDetailQuery(instDispatchingDetail);
        model.addAttribute("instDispatchingList",instDispatchingList);
        model.addAttribute("instDispatchingDetails", instDispatchingDetails);
        return "shipments/InstDispatchingSubmit/pageSelect";
    }
    /**
     * 提交数据
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/submit")
    public JsonResult submit(Integer id) {
        startPage();
        int i = instDispatchingCreateService.sumitInstDispatching(id);
        return jsonResult(i);
    }
    /**
     * 删除数据
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    public JsonResult del(Integer id) {
        startPage();
        int i = instDispatchingCreateService.delInstDispatching(id);
        return jsonResult(i);
    }
    /**
     *编辑明细
     * @param model
     * @return
     */
    @GetMapping("/listDetailEdit/{id}")
    public String listDetailEdit(Model model,@PathVariable("id") Integer id) {
        InstDispatchingList instDispatchingList = instDispatchingRepository.getOne(id);
        InstDispatchingDetail instDispatchingDetail = new InstDispatchingDetail();
        instDispatchingDetail.setSheetId(id);
        List<InstDispatchingDetail> instDispatchingDetails = instDispatchingCreateService.findinstDispatchingDetailQuery(instDispatchingDetail);
        model.addAttribute("instDispatchingList",instDispatchingList);
        for (int i = 0; i <instDispatchingDetails.size() ; i++) {
            if (instDispatchingDetails.get(i).getSpecial().equals("机械")){
                model.addAttribute("instDispatchingDetailsJx", instDispatchingDetails.get(i));
            }
            if (instDispatchingDetails.get(i).getSpecial().equals("流体")){
                model.addAttribute("instDispatchingDetailsLt", instDispatchingDetails.get(i));
            }
            if (instDispatchingDetails.get(i).getSpecial().equals("自动化")){
                model.addAttribute("instDispatchingDetailsZdh", instDispatchingDetails.get(i));
            }
        }
        return "shipments/InstDispatchingSubmit/pageEdit";
    }
    /**
     * 修改发货计划主单
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/editDispatching")
//    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult editDispatching(InstDispatchingList instDispatchingList,Integer[] idd, String[] special, String[] installPrincipal, String[] installTime, Integer[] isDebugging, String[] installRemark) throws ParseException {
        int i = instDispatchingCreateService.editAll(instDispatchingList,idd,special,installPrincipal,installTime,isDebugging,installRemark);
        return jsonResult(1);
    }
    /**
     *审核页面主页
     * @param model
     * @return
     */
    @GetMapping("/examine")
    public String examine(Model model) {
        return "shipments/InstDispatchingExamine/page";
    }
    /**
     * 审核数据
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/examine")
    public JsonResult examine(Integer id) {
        startPage();
        int i = instDispatchingCreateService.examineInstDispatching(id);
        return jsonResult(i);
    }
    /**
     * 驳回
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
//    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult reject(InstDispatchingList instDispatchingList){
        int i = instDispatchingCreateService.reject(instDispatchingList);
        return jsonResult(1);
    }
    /**
     *安装任务反馈页面主页
     * @param model
     * @return
     */
    @GetMapping("/installPage")
    public String installPage(Model model) {
        return "shipments/InstInstall/installPage";
    }
    /**
     *安装任务反馈页面查询
     * @param model
     * @return
     */
    @GetMapping("/installPageSelect")
    public String installPageSelect(Model model) {
        return "shipments/InstInstallSelect/installPageSelect";
    }
    /**
     * 安装反馈明细集合
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/installDetail")
    public JsonResult installDetail(InstDispatchingDetail instDispatchingDetail) {
        startPage();
        instDispatchingDetail.setExpCount(1);
        List<InstDispatchingDetail> list = instDispatchingCreateService.findinstDispatchingDetailQuery(instDispatchingDetail);
        return jsonResult(list);
    }
    /**
     * 安装反馈集合
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/installList")
    public JsonResult installList(InstDispatchingList instDispatchingList) {
        startPage();
        instDispatchingList.setSubStatus(3);
        List<InstDispatchingList> list = instDispatchingCreateService.findinstDispatchingQuery(instDispatchingList);
        return jsonResult(list);
    }
    /**
     * 安装文件列表
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/installFile")
    public JsonResult installFile(InstDispatchingFile instDispatchingFile) {
        startPage();
        //调试
        instDispatchingFile.setType(1);
        List<InstDispatchingFile> list = instDispatchingCreateService.findinstDispatchingFileQuery(instDispatchingFile);
        return jsonResult(list);
    }
    /**
     * 安装调试进度
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/installDetailSave")
    public JsonResult installDetailSave(InstDispatchingDetail instDispatchingDetail) {
        instDispatchingCreateService.saveInstDispatchingDetailnstall(instDispatchingDetail);
        return jsonResult(1);
    }
    /**
     * 增加发货计划主单
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/addDispatching")
//    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult addProduction(InstDispatchingList instDispatchingList, String[] special, String[] installPrincipal, String[] installTime, Integer[] isDebugging, String[] installRemark) throws ParseException {
       int i = instDispatchingCreateService.saveAll(instDispatchingList,special,installPrincipal,installTime,isDebugging,installRemark);
        return jsonResult(1);
    }
    /**
     * 调试反馈集合
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/debuggingList")
    public JsonResult debuggingList(InstDispatchingList instDispatchingList) {
        startPage();
        instDispatchingList.setIsDebugging(1);
        instDispatchingList.setSubStatus(3);
        List<InstDispatchingList> list = instDispatchingCreateService.findinstDispatchingQuery(instDispatchingList);
        return jsonResult(list);
    }
    /**
     * 调试反馈明细集合
     *1、下达生产令任务时，生成调试任务；（徐一贺）
     * 2、创建安装派工单时，取消调试任务的设置；（安装派工单和调试没关系了）
     * 3、调试任务反馈页面，读调试任务表的数据，不是读安装派工的数据了；
     * 4、调试任务反馈的功能不变，还是反馈状态和上传附件；但是所有的项目，不仅是非设计类了；
     * 5、将所有已经下达生产令的项目，初始化到调试任务表，调试节点的状态按老看板系统进行初始化；
     * 6、按老看板系统初始化上传的附件；
     * 7、核对一下，调试节点的状态和销售系统不一致的项目，统计出来；（马超）
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/debuggingDetail")
    public JsonResult debuggingDetail(InstDebuggingDetail instDebuggingDetail) {
        startPage();
        List<InstDebuggingDetail> list = instDispatchingCreateService.findinstDispatchQuery(instDebuggingDetail);
        return jsonResult(list);
    }
    /**
     * 维护调试进度
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/debuggingDetailSave")
    public JsonResult debuggingDetailSave(InstDebuggingDetail instDebuggingDetail) {
        instDispatchingCreateService.saveInstDispatchingDetail(instDebuggingDetail);
        return jsonResult(1);
    }
    /**
     * 调试文件列表
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/debuggingFile")
    public JsonResult debuggingFile(InstDebuggingFile instDebuggingFile) {
        startPage();
        List<InstDebuggingFile> list = instDispatchingCreateService.findinstDebuggingFileQuery(instDebuggingFile);
        return jsonResult(list);
    }
    /**
     * 导入调试附件
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/uploadFileExp")
    public JsonResult uploadFileExp(InstDebuggingFile instDebuggingFile,@RequestParam("file") MultipartFile file) {
        instDispatchingCreateService.uploadFileExp(instDebuggingFile, file);
        return success();
    }
    /**
     * 导入安装附件
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/uploadFileExpStall")
    public JsonResult uploadFileExpStall(InstDispatchingFile instDispatchingFile,@RequestParam("file") MultipartFile file) {
        instDispatchingCreateService.uploadFileExpStall(instDispatchingFile, file);
        return success();
    }
    /**
     * 修改说明文件安装
     */
    @ResponseBody
    @PostMapping("/updExp")
    public JsonResult updDubuggingExp(InstDispatchingFile instDispatchingFile) {
        return jsonResult(instDispatchingCreateService.updExplainById(instDispatchingFile));
    }
    /**
     * 修改说明文件调试
     */
    @ResponseBody
    @PostMapping("/updExpDebugging")
    public JsonResult updExpDebugging(InstDebuggingFile instDebuggingFile) {
        return jsonResult(instDispatchingCreateService.updExpDebugging(instDebuggingFile));
    }
    /**
     * 删除附件安装
     */
    @ResponseBody
    @PostMapping("/delExp")
    public JsonResult delExp(InstDispatchingFile instDispatchingFile) {
        return jsonResult(instDispatchingCreateService.delExp(instDispatchingFile));
    }
    /**
     * 删除附件调试
     */
    @ResponseBody
    @PostMapping("/delExpDebugging")
    public JsonResult delExpDebugging(InstDebuggingFile instDebuggingFile) {
        return jsonResult(instDispatchingCreateService.delExpDebugging(instDebuggingFile));
    }
    @GetMapping("/downloadProjectFileCoatChange/{id}")
    public void downloadProjectFileCoatChange(
            @PathVariable("id") Integer id, HttpServletResponse httpServletResponse) {
        try {
            InstDebuggingFile instDebuggingFile = instDebuggingFileRepository.getOne(id);
            String filename = instDebuggingFile.getUrl();
            FileUtils.downloadFile(filename, httpServletResponse);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
